package com.kunpan.admin.common.utils;

import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.security.SecureRandom;
import java.util.Base64;

public class RefreshTokenUtils {

    private static final SecureRandom secureRandom = new SecureRandom();
    // 使用 Base64 编码方案生成48字节的refreshToken
    public static String createRefreshToken () {
        byte[] randomBytes = new byte[48];
        secureRandom.nextBytes(randomBytes);
        return Base64.getUrlEncoder().withoutPadding().encodeToString(randomBytes);
    }

    //根据请求信息生成设备指纹
    public static String buildDeviceFingerprint(HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        String ua = request.getHeader("User-Agent");
        return DigestUtils.md5DigestAsHex((ip + ua).getBytes());
    }
}
